//
//  Log.c
//
//  Created by 王 嗣宏 on 13-6-29.
//  Copyright (c) 2013年 王 嗣宏. All rights reserved.
//

#include <stdio.h>
#include "Log.h"
#include <stdarg.h>

int log_level_g = LALWAYS;

void SetLogLevel(LOG_LEVEL level)
{
    log_level_g = level;
    return;
}

void Log(LOG_LEVEL level, const char * massage, ...)
{
    char out_buff[1024] = {0};
    
    va_list args;
    va_start(args,massage);
    vsnprintf(out_buff, sizeof(out_buff), massage, args);
    va_end(args);
    
    switch (level) {
        case LALWAYS:
            printf("[LOG] ALWAYS: %s\n", out_buff);
            break;
        case LERROR:
            if (log_level_g > LERROR)
                printf("[LOG] ERROR: %s\n", out_buff);
            break;
        case LWARNING:
            if (log_level_g > LWARNING)
                printf("[LOG] WARNING: %s\n", out_buff);
            break;
        case LINFO:
            if (log_level_g > LINFO)
                printf("[LOG] INFO: %s\n", out_buff);
            break;
        case LMASSAGE:
            if (log_level_g > LMASSAGE)
                printf("[LOG] MASSAGE: %s\n", out_buff);
            break;
        default:
            break;
    }
        
    return;
}

void LogD(LOG_LEVEL level, int line, const char* func, const char* file, const char * massage, ...)
{
    char out_buff[1024] = {0};
    
    va_list args;
    va_start(args,massage);
    vsnprintf(out_buff, sizeof(out_buff), massage, args);
    va_end(args);
    
    switch (level) {
        case LALWAYS:
            printf("[LOG ALWAYS] %s line:%d func:%s file:%s\n", out_buff, line, func, file);
            break;
        case LERROR:
            if (log_level_g > LERROR)
                printf("[LOG ERROR] %s line:%d func:%s file:%s\n", out_buff, line, func, file);
            break;
        case LWARNING:
            if (log_level_g > LWARNING)
                printf("[LOG WARNING] %s line:%d func:%s file:%s\n", out_buff, line, func, file);
            break;
        case LINFO:
            if (log_level_g > LINFO)
                printf("[LOG INFO] %s line:%d func:%s file:%s\n", out_buff, line, func, file);
            break;
        case LMASSAGE:
            if (log_level_g > LMASSAGE)
                printf("[LOG MASSAGE] %s line:%d func:%s file:%s\n", out_buff, line, func, file);
            break;
        default:
            break;
    }
        
    return;
}
